package com.excesys.exsecs.alarm.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.excesys.exsecs.alarm.dto.AlarmEnableDto;
import com.excesys.exsecs.alarm.entity.AlarmEnable;
import com.excesys.exsecs.alarm.entity.AlarmEnableBind;
import com.excesys.exsecs.alarm.mapper.AlarmEnableBindMapper;
import com.excesys.exsecs.alarm.mapper.AlarmEnableMapper;
import com.excesys.exsecs.alarm.service.AlarmEnableService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Optional;

/**
 * 告警类型配置表
 *
 * @author lxc
 * @date 2024-10-14 16:40:36
 */
@Service
@RequiredArgsConstructor
public class AlarmEnableServiceImpl extends ServiceImpl<AlarmEnableMapper, AlarmEnable> implements AlarmEnableService {

	private final AlarmEnableBindMapper alarmEnableBindMapper;
	@Override
	public AlarmEnableDto getInfoByName(String name) {
		return this.baseMapper.getInfoByName(name);
	}

	@Override
	public IPage<AlarmEnableDto> getAlarmEnableInfoPage(Page page, AlarmEnable alarmEnable) {
		return this.baseMapper.getAlarmEnableInfoPage(page,alarmEnable);
	}

	@Override
	public AlarmEnableDto getInfoByid(String id) {
		return  this.baseMapper.getInfoByid(id);
	}

	@Override
	public boolean saveAlarmEnable(AlarmEnableDto alarmEnableDto) {
		AlarmEnable alarmEnable = new AlarmEnable();
		BeanUtils.copyProperties(alarmEnableDto, alarmEnable);
		baseMapper.insert(alarmEnable);
		Optional.ofNullable(alarmEnableDto.getAlarmEnableBindList()).ifPresent(alarmEnableBindList -> {
			alarmEnableBindList.stream().map(alarmEnableBind -> {
				alarmEnableBind.setEnableId(alarmEnable.getEnableId());
				return alarmEnableBind;
			}).forEach(alarmEnableBindMapper::insert);
		});
		return  true;
	}

	@Override
	public boolean updateAlarmEnable(AlarmEnableDto alarmEnableDto) {
		AlarmEnable alarmEnable = new AlarmEnable();
		BeanUtils.copyProperties(alarmEnableDto, alarmEnable);
		alarmEnableBindMapper.delete(Wrappers.<AlarmEnableBind>update().lambda().eq(AlarmEnableBind::getEnableId, alarmEnableDto.getEnableId()));
		Optional.ofNullable(alarmEnableDto.getAlarmEnableBindList()).ifPresent(alarmEnableBindList -> {
			alarmEnableBindList.stream().map(alarmEnableBind -> {
				alarmEnableBind.setEnableId(alarmEnableDto.getEnableId());
				return alarmEnableBind;
			}).forEach(alarmEnableBindMapper::insert);
		});
		return SqlHelper.retBool(this.getBaseMapper().updateById(alarmEnable));
	}

	@Override
	public boolean removeAlarmEnableById(Serializable id) {
		this.alarmEnableBindMapper.deleteByEnableId(id+"");
		return SqlHelper.retBool(this.getBaseMapper().deleteById(id));
	}
}